package com.example.cinema.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.recyclerview.widget.RecyclerView;

import com.example.cinema.BR;
import com.example.cinema.bindingdata.LiveTableData;
import com.example.cinema.bindingdata.LiveTypeData;
import com.example.cinema.bindingdata.TypeData;

import java.util.List;

public class RecyclerTypeAdapter<T>  extends RecyclerView.Adapter<ItemHandler> {

    private LayoutInflater inflater;
    private Context context;
    private List<T> datalist;
    private int layoutid;

    public RecyclerTypeAdapter(Context context, List<T> datalist, int layoutid) {
        this.inflater = LayoutInflater.from(context);
        this.context = context;
        this.datalist = datalist;
        this.layoutid = layoutid;
    }

    @NonNull
    @Override
    public ItemHandler onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ViewDataBinding dataBinding =  DataBindingUtil.inflate(inflater,layoutid,parent,false);
        ItemHandler handle = new ItemHandler(dataBinding);
        return handle;
    }

    @Override
    public void onBindViewHolder(@NonNull ItemHandler holder, int position) {
        LiveTypeData data = (LiveTypeData) datalist.get(position);
        holder.dataBinding.setVariable(BR.livedatas,data);
        holder.dataBinding.getRoot().setTag(data.typetid);
        holder.dataBinding.getRoot().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(setOnItemClick!=null)
                {
//                    setOnItemClick.onitemClick(v,datalist.get(position));
                    setOnItemClick.onitemClick(v,position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return datalist.size();
    }

    public interface setOnItemClick{
        void onitemClick(View view,Object object);
    }
    private setOnItemClick setOnItemClick;
    public void setOnItemClickListener(setOnItemClick itemClick)
    {
        setOnItemClick = itemClick;
    }
}
class ItemHandler extends RecyclerView.ViewHolder{
    public ViewDataBinding dataBinding;
    public ItemHandler(@NonNull ViewDataBinding itemView) {
        super(itemView.getRoot());
        dataBinding = itemView;
    }
}